<?php

namespace app\admin\controller;

use app\helper\Helper;
use app\model\DepartModel;
use think\Db;
use think\Validate;

class Depart extends Base
{
    /**
     * 列表展示
     * @return \think\response\View
     */
    public function index()
    {
        return view();
    }

    /**
     * 列表接口
     */
    public function departList()
    {
        $param = $this->request->param();
        $rule = [
            'depart_name|部门名称'=>'max:10',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            echo json_encode(['code'=>500,'msg'=>$validate->getError()]);die;
        }

        $page = isset($param['page']) ? intval($param['page']) : 1;
        $pageSize = isset($param['limit']) ? intval($param['limit']) : 10;
        $where = 'status = 1';
        if(!empty($param['depart_name'])){
            $where .= ' and depart_name like "%'.$param['depart_name'].'%"';
        }
        $count = Db::name('depart')->where($where)->count();
        $tol = ($page - 1) * $pageSize + 1;
        $list = Db::name('depart')
            ->where($where)
            ->where('id', '>=', $tol)
            ->limit($pageSize)
            ->select();
         return ['code'=>0,'msg'=>'','count'=>$count,'data'=>$list];
    }

    /**
     * 添加 编辑页面
     * @return \think\response\View
     */
    public function saveDepartInfo()
    {
        $param = $this->request->param();
        if(!empty($param['id'])){
            $departId = $param['id'];
            $info = Db::name('depart')->field('id,depart_name,status')->where(['id'=>$departId])->find();
            $this->assign('info',$info);
        }
        return view();
    }

    public function saveInfo()
    {
        $params=$this->request->param();
        $rule = [
            'depart_name|部门名称'=>'require|max:10',
            'status|状态'=>'require|number',
        ];

        $validate = new Validate($rule);
        if(!$validate->check($params)){
            echo json_encode(['code'=>400,'msg'=>$validate->getError()]);die;
        }
        $data=[
            'depart_name'=>$params['depart_name'],
            'status'=>$params['status'],
        ];

        if(!empty($params['id'])){
            $data['update_ts'] = date('Y-m-d H:i:s',time());
            $res = Db::name('depart')->where(['id'=>$params['id']])->update($data);
        }else{
            $data['created_ts'] = date('Y-m-d H:i:s',time());
            $data['update_ts'] = date('Y-m-d H:i:s',time());
            $res = Db::name('depart')->insert($data);
        }
        if($res){
            echo json_encode(['code'=>200,'msg'=>'保存成功','data'=>$res]);die;
        }else{
            echo json_encode(['code'=>400,'msg'=>'保存失败','data'=>$res]);die;
        }

    }




    /**
     * 删除部门
     */
    public function departDel()
    {
        $param=$this->request->param();
        $rule = [
            'id'=>'require|number',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            echo json_encode(['code' => 500,'msg'=>$validate->getError()]);die;
        }
        $res = Db::name('depart')->delete($param['id']);
        if($res){
            echo json_encode(['code' => 200,'msg'=>'删除成功']);die;

        }else{
            echo json_encode(['code' => 500,'msg'=>'删除失败']);die;
        }
    }
}